/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.*;
import org.junit.Test;

// 注解
@SuppressWarnings({"restriction"})
/** @author wangfupeng */
/**
 * 注释
*/
public class CacheTranslateUtil {

    public static TextTranslateResponse translate(String translate, String sourceLanguage, String targetLanguage) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKIDhcR5mNZHUVL3ZtuLMI4gUCtPp47ycHj4", "PglkZFMNDtxrpgOvHRQZcYL1ykR1Skda");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tmt.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            TmtClient client = new TmtClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            TextTranslateRequest req = new TextTranslateRequest();
            req.setSourceText(translate);
            req.setSource(sourceLanguage);
            req.setTarget(targetLanguage);
            req.setProjectId(0L);
            // 返回的resp是一个TextTranslateResponse的实例，与请求对象对应
            TextTranslateResponse resp = client.TextTranslate(req);
            // 输出json格式的字符串回包
            System.out.println(TextTranslateResponse.toJsonString(resp));
            return resp;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return new TextTranslateResponse();
    }

    public static String translateToString(String translate, String sourceLanguage, String targetLanguage) {
        try {
            String translateResult = translate(translate,sourceLanguage,targetLanguage).getTargetText();
            return translateResult==null?"":translateResult;
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Test
    public void translateTest(){
        System.out.println(translate("public static final Logger logger = Logger Factory.get Logger(Cache Aop Util. class);","en","zh"));
    }

}

